package com.dm.dm_project.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * [说明/描述]
 *
 * @author 徐杜伟
 * @version 版本号
 * @date 2023/10/30
 */
@Slf4j
@Component
public class RunJob {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Scheduled(initialDelay = 10*1000L,fixedDelay = Long.MAX_VALUE)
    public void runJob(){
        new Thread(this::runMethod).start();
        new Thread(this::runMethod).start();
    }

    private void runMethod() {
        Random random = new Random();
        while (true){
            int interval = random.nextInt(20) + 1;
            log.info("stop time : {}s ;thread:{}",interval,Thread.currentThread().getName());
            try {
                List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM WB_METER LIMIT 50;");
                if(!CollectionUtils.isEmpty(list)){
                    jdbcTemplate.execute("INSERT INTO OPT_TOTAL VALUES(LOWER(SYS_GUID()),'java',SYSDATE());");
                }
                Thread.sleep(interval * 1000);
            } catch (InterruptedException e) {
                log.error("error: ",e);
                break;
            }
        }
    }
}
